package cn.ds.assets.module.assets.service.metadataModelBusiness;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.metadataModelBusiness.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataModelBusiness.BaseMetadataModelBusinessDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.metadataModelBusiness.BaseMetadataModelBusinessConvert;
import cn.ds.assets.module.assets.dal.mysql.metadataModelBusiness.BaseMetadataModelBusinessMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 元数据管理-元模型-业务属性 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseMetadataModelBusinessServiceImpl implements BaseMetadataModelBusinessService {

    @Resource
    private BaseMetadataModelBusinessMapper baseMetadataModelBusinessMapper;

    @Override
    public Long createBaseMetadataModelBusiness(BaseMetadataModelBusinessCreateReqVO createReqVO) {
        // 插入
        BaseMetadataModelBusinessDO baseMetadataModelBusiness = BaseMetadataModelBusinessConvert.INSTANCE.convert(createReqVO);
        baseMetadataModelBusinessMapper.insert(baseMetadataModelBusiness);
        // 返回
        return baseMetadataModelBusiness.getId();
    }

    @Override
    public void updateBaseMetadataModelBusiness(BaseMetadataModelBusinessUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseMetadataModelBusinessExists(updateReqVO.getId());
        // 更新
        BaseMetadataModelBusinessDO updateObj = BaseMetadataModelBusinessConvert.INSTANCE.convert(updateReqVO);
        baseMetadataModelBusinessMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseMetadataModelBusiness(Long id) {
        // 校验是否存在
        validateBaseMetadataModelBusinessExists(id);
        // 逻辑删除
        baseMetadataModelBusinessMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseMetadataModelBusinessExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseMetadataModelBusinessDO dataDo = baseMetadataModelBusinessMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseMetadataModelBusinessDO getBaseMetadataModelBusiness(Long id) {
        return baseMetadataModelBusinessMapper.selectById(id);
    }

    @Override
    public List<BaseMetadataModelBusinessDO> getBaseMetadataModelBusinessList(Collection<Long> ids) {
        return baseMetadataModelBusinessMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseMetadataModelBusinessDO> getBaseMetadataModelBusinessPage(BaseMetadataModelBusinessPageReqVO pageReqVO) {
        return baseMetadataModelBusinessMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseMetadataModelBusinessDO> getBaseMetadataModelBusinessList(BaseMetadataModelBusinessExportReqVO exportReqVO) {
        return baseMetadataModelBusinessMapper.selectList(exportReqVO);
    }

}
